package helperClassSites;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import utils.Utils;

public class CreateTour {
	public static void CreateTourFiles(String path, int xlsSheet){
		try {
			Workbook workbook = Workbook.getWorkbook(new File(
					"C:\\Documents and Settings\\Administrator\\My Documents\\Dropbox\\CTA\\XLS\\CTA.xls"));
			Sheet sheet = workbook.getSheet(xlsSheet);
			Cell[] count = sheet.getColumn(0);
			Sheet other = workbook.getSheet(8);
			Cell codeCell = other.getCell(0, 0);
			String code = codeCell.getContents();
			for (int i = 1; i < count.length; i++) {
				Cell siteNameCell = sheet.getCell(9, i);
				String sitename = siteNameCell.getContents();
				File replacedFile = new File(path + sitename + ".html");
				replacedFile.delete();
				FileWriter fstream = new FileWriter(path + sitename + ".html");
				BufferedWriter textFile = new BufferedWriter(fstream);

				textFile.write("<!--\n");
				textFile.write("textFile.write(\"  Copyright (c) 2009 Google inc.\n");
				textFile.write("\n");
				textFile.write("  You are free to copy and use this sample.\n");
				textFile.write("  License can be found here: http://code.google.com/apis/ajaxsearch/faq/#license\n");
				textFile.write("-->\n");
				textFile.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n");
				textFile.write("<html xmlns=\"http://www.w3.org/1999/xhtml\">\n");
				textFile.write("  <head>\n");
				textFile.write("    <meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\"/>\n");
				Cell passNameCell = sheet.getCell(4, i);
				String passNameEN = passNameCell.getContents();
				String replaced = Utils.replaceUmlautsDeutsch(passNameEN);
				textFile.write("    <meta name=\"Description\" content=\"3D tour of the "
						+ replaced
						+ ". This tour is based on google earth and let you explore the "
						+ replaced
						+ " by sitting back and relax yourself. There are around 480 paved passes on www.cyclingthealps.com. Cycling the Alps gives you a virtual experience about all the passes in the alps.\" />\n");
				textFile.write("    <title>" + replaced + " 3D Tour</title>\n");
				textFile.write("	\n");
				textFile.write("  <script type=\"text/javascript\"> \n");
				textFile.write("  var googleMapsApiKeys = []; \n");
				textFile.write("  googleMapsApiKeys['http://cta.wegewijs.info/'] = 'ABQIAAAAM4pWLS64lWH2T51CW5FpkRTBicl7mDxxQ0ZVLXxnkdyYsvjN2xRD0jVY52fdXUpjz9rANIBJiWPV0w';\n");
				textFile.write("  googleMapsApiKeys['http://www.wegewijs.info/'] = 'ABQIAAAAM4pWLS64lWH2T51CW5FpkRT-xS-a97Jv2dVLwJNVV1w_TZi6pBTQpY11tZ1Y4fardIpuI2sHtbfrcQ';\n");
				textFile.write("  googleMapsApiKeys['http://www.cyclingthealps.com/'] = 'ABQIAAAAM4pWLS64lWH2T51CW5FpkRRu0hKdmJtwqUpy0N8zchdihiwGgRT7yprgzTDNAnphI0d4JwZ71whrhA';\n");
				textFile.write("  googleMapsApiKeys['http://cta.cyclingthealps.com/'] = 'ABQIAAAAM4pWLS64lWH2T51CW5FpkRRxWkNdpiRta-ntjL1r0v29A2_XuBQriWGZ66izn5yupLMismdpENe4MA';\n");
				textFile.write("  googleMapsApiKeys['http://alpspasses.appspot.com/'] = 'ABQIAAAAM4pWLS64lWH2T51CW5FpkRTn0IpzAJXD01_fqfQFNim4xN6saxRtEL98s25b9oDrAP9cOhoAO2sfMQ';\n");
				textFile.write(code + " \n");
				textFile.write(" </script>\n");
				textFile.write("    \n");
				
				textFile.write("<script type=\"text/javascript\">var gaJsHost = ((\"https:\" == document.location.protocol) ? \"https://ssl.\" : \"http://www.\");document.write(unescape(\"%3Cscript src='\" + gaJsHost + \"google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E\"));</script>");
				textFile.write("    \n");
				textFile.write("<script type=\"text/javascript\">  var _gaq = _gaq || [];  _gaq.push(['_setAccount', 'UA-13084846-1']);  _gaq.push(['_trackPageview']);  (function() {    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);  })();</script>");
				textFile.write("    \n");
				
				textFile.write("    <!-- We'll need to walk the DOM looking for features of a certain type later -->\n");
				textFile.write("    <script src=\"http://earth-api-samples.googlecode.com/svn/trunk/lib/kmldomwalk.js\" type=\"text/javascript\"></script>\n");
				textFile.write("\n");
				textFile.write("    <script type=\"text/javascript\">\n");
				textFile.write("    var ge;\n");
				textFile.write("    \n");
				textFile.write("    var tour = null;\n");
				textFile.write("    \n");
				textFile.write("    google.load(\"earth\", \"1\");\n");
				textFile.write("    \n");
				textFile.write("    function init() {\n");
				textFile.write("      google.earth.createInstance('map3d', initCallback, failureCallback);\n");
				textFile.write("    }\n");
				textFile.write("    \n");
				textFile.write("    function initCallback(instance) {\n");
				textFile.write("      ge = instance;\n");
				textFile.write("      ge.getWindow().setVisibility(true);\n");
				textFile.write("    \n");
				textFile.write("      // add a navigation control\n");
				textFile.write("      ge.getNavigationControl().setVisibility(ge.VISIBILITY_AUTO);\n");
				textFile.write("    \n");
				textFile.write("      // add some layers\n");
				textFile.write("      ge.getLayerRoot().enableLayerById(ge.LAYER_BORDERS, true);\n");
				textFile.write("      ge.getLayerRoot().enableLayerById(ge.LAYER_ROADS, true);\n");
				textFile.write("    \n");
				textFile.write("      // create the tour by fetching it out of a KML file\n");
				Cell linkCell = sheet.getCell(32, i);
				String link = linkCell.getContents();
				textFile.write("      var href = '" + link + "';\n");
				textFile.write("    \n");
				textFile.write("      google.earth.fetchKml(ge, href, function(kmlObject) {\n");
				textFile.write("        if (!kmlObject) {\n");
				textFile.write("          // wrap alerts in API callbacks and event handlers\n");
				textFile.write("          // in a setTimeout to prevent deadlock in some browsers\n");
				textFile.write("          setTimeout(function() {\n");
				textFile.write("            alert('Bad or null KML.');\n");
				textFile.write("          }, 0);\n");
				textFile.write("          return;\n");
				textFile.write("        }\n");
				textFile.write("    \n");
				textFile.write("        // Show the entire KML file in the plugin.\n");
				textFile.write("        ge.getFeatures().appendChild(kmlObject);\n");
				textFile.write("    \n");
				textFile.write("        // Walk the DOM looking for a KmlTour\n");
				textFile.write("        walkKmlDom(kmlObject, function() {\n");
				textFile.write("          if (this.getType() == 'KmlTour') {\n");
				textFile.write("            tour = this;\n");
				textFile.write("			enterTour();\n");
//				if (path.endsWith("tour\\")) {
//					textFile.write("			playTour();\n");
//				}
				textFile.write("            return false; // stop the DOM walk here.\n");
				textFile.write("          }\n");
				textFile.write("        });\n");
				textFile.write("      });\n");
				textFile.write("    \n");
				textFile.write("      document.getElementById('installed-plugin-version').innerHTML =\n");
				textFile.write("        ge.getPluginVersion().toString();\n");
				textFile.write("    }\n");
				textFile.write("    \n");
				textFile.write("    function failureCallback(errorCode) {\n");
				textFile.write("    }\n");
				textFile.write("    \n");
				textFile.write("    function enterTour() {\n");
				textFile.write("      if (!tour) {\n");
				textFile.write("        alert('No tour found!');\n");
				textFile.write("        return;\n");
				textFile.write("      }\n");
				textFile.write("      ge.getTourPlayer().setTour(tour);\n");
				textFile.write("    }\n");
				textFile.write("    function playTour() {\n");
				textFile.write("  	  ge.getTourPlayer().play();\n");
				textFile.write("  }\n");
				textFile.write("    </script>\n");
				textFile.write("  </head>\n");
				textFile.write("  <body onload=\"init()\" style=\"font-family: arial, sans-serif; font-size: 13px; border: 0; padding: 0; overflow: hidden; margin: 0;\">\n");
				textFile.write("    <div id=\"sample-ui\"></div>\n");
				textFile.write("      <div id=\"map3d\" style=\"position:absolute;;border:0px;width:100%;height:100%;;\"></div>\n");
				textFile.write("  </body>\n");
				textFile.write("</html>\n");
				textFile.close();
			}
		} catch (BiffException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}
